package tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;

public class TCPUtils {

	private static final int SIZE = 1024;
	
	private static OutputStream out;
    private static InputStream in;

	private static long images_grabbed = 0;
	private static boolean completed = true;
	
	public final static void tcpSendBytes(Socket socket, byte[] tab) throws IOException {
		out = socket.getOutputStream();
		// envoyer la taille total du packet
		out.write(ByteBuffer.allocate(4).putInt(tab.length).array());
		
		//System.out.println("j'envois "+tab.length);
		// mettre l'image en morceaux
		ByteArrayIterable byte_iterable = new ByteArrayIterable(tab, SIZE);
		for(byte[] buf : byte_iterable) {
			out.write(buf);
			out.flush();
		}
	}

	public final static byte[] tcpWaitingHeader(Socket socket) throws IOException {
		byte[] buf = new byte[4];
		in = socket.getInputStream();
		in.read(buf);
		int bufSize = ByteBuffer.wrap(buf).getInt();
		
		buf = new byte[bufSize];
		in.read(buf);
		
		return buf;
	}
	
	public final static byte[] tcpWaitingData(Socket socket) throws IOException {
		byte[] buf = new byte[4];
		in = socket.getInputStream();
		in.read(buf);
		int total_size = ByteBuffer.wrap(buf).getInt();
		//System.out.println(buf);
		//System.out.println("je reçois "+total_size+" bytes");
		
		
		if(total_size != 230400) {
			if(completed == true)
				System.out.println("skipping image "+images_grabbed);
			completed = false;
			return null;
		}
			
		byte[] res = new byte[total_size];
		
		// lire les packets
		int i = 0;
		int read = 0;
		do {
			buf = new byte[SIZE];
			read = in.read(buf);
			total_size -= SIZE;
			System.arraycopy(buf, 0, res, i*SIZE, SIZE);
			if(read == 1024)
				i++;
		} while(total_size >= SIZE);
		
		images_grabbed++;
		completed = true;
		
		/*int read = 0;
		for(int i=0; i<225; i++) {
			buf = new byte[SIZE];
			read = in.read(buf);
			System.arraycopy(buf, 0, res, i*SIZE, read);
			if(read != 1024)
				i--;
			//if(i == 224)
				//System.out.println(new String(buf));

			// byte par byte
			/*buf = new byte[SIZE];
			int total = 0;
			int read = 0;
			while(total < SIZE) {
				read = in.read(buf, total, SIZE - total);
				total += read;
			}
			System.out.println(read);*/
		//}
		
		// last packet
		//System.out.println(total_size);
		//buf = new byte[total_size];
		//in.read(buf);
		//System.arraycopy(buf, 0, res, i*SIZE, total_size);
		//System.out.println("dernier paquet "+new String(buf));
		
		//System.out.println("Waiting end");
		
		return res;
	}


	public final static byte[] tcpWaitingData_(Socket socket) throws IOException {
		int read = 0;
		int total_size = 0;
		byte[] buf = new byte[4];
		in = socket.getInputStream();
		
		while(read != 4) {
			in.read(buf, read, 1);
			read++;
		}
		total_size = ByteBuffer.wrap(buf).getInt();
		System.out.println("je recois "+total_size);
		
		buf = new byte[total_size];
		read = 0;
		while(read != total_size) {
			in.read(buf, read, 1);
			read++;
		}

		images_grabbed++;
		
		return buf;
	}
	
}
